import './App.css';
import routes from "./router";
import { useLocation, useNavigate, useRoutes} from "react-router-dom";
import {useEffect} from "react";

function BeforeEnter() {
    const outlet = useRoutes(routes);
    const location = useLocation();
    let token = localStorage.getItem("token");
    if(location.pathname !== "/login" && !token) {
        return <ToLogin/>
    }
    if(token && location.pathname === "/login") {
        return <>
            <ToHome/>
        </>
    }
    return outlet;
}

function ToLogin() {
    const navigateTo = useNavigate()
    //加载完这个组件实现跳转
    useEffect(()=>{
        navigateTo("login")
    })
    return <div></div>
}

function ToHome() {
    const navigateTo = useNavigate()
    //加载完这个组件实现跳转
    useEffect(()=>{
        navigateTo("/")
    })
    return <div></div>
}

function App() {
  return (
    <div className="App">
      <header className="App-header">
          <BeforeEnter></BeforeEnter>
      </header>
    </div>
  );
}

export default App;
